/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.click.app.webapp.action;

import com.click.app.model.Laundry;
import com.click.app.model.LaundryPayment;
import com.click.app.model.LaundryRequest;
import com.click.app.model.PeriodPaymentLaundry;
import com.click.app.service.LaundryManager;
import com.click.app.service.PeriodPaymentLaundryManager;
import com.click.app.webapp.reportObjects.RowEntry;
import com.click.app.webapp.reportObjects.Worksheet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author ronald
 */
public class LaundryAction extends BaseAction {
    private List<Laundry> laundryList;
    private LaundryManager laundryManager;

    private Long id;
    private Laundry laundry;
    private List<PeriodPaymentLaundry> periodPaymentLaundryList;
    private String choicedPerPayLaundry;

    private PeriodPaymentLaundry periodPaymentLaundry;
    private PeriodPaymentLaundryManager periodPaymentLaundryManager;
    private Worksheet workSheet;
    private List<RowEntry> listRowEntries;
    
    public String list(){
        getSession().removeAttribute("lPid");
        laundryList = laundryManager.getAll();
        return SUCCESS;
    }

    public String choicedLaundry(){
        //if(id == null)
        //    id = (Long)getSession().getAttribute("lPid");

        if (id != null){
            laundry = laundryManager.get(id);
            periodPaymentLaundryList = laundry.getPeriodPaymentLaundryList();
        }
        return SUCCESS;
    }

    public String generateReport(){
        if(!choicedPerPayLaundry.equals("-1")){
           periodPaymentLaundry = periodPaymentLaundryManager.get(new Long(choicedPerPayLaundry));
           List<RowEntry> listRowEntryEntry = getListRowEntryEntry(periodPaymentLaundry.getLaundryRequestList());
           List<RowEntry> listRowEntryPayments = getListRowEntryPayment(periodPaymentLaundry.getLaundryPaymentList());
           workSheet = new Worksheet(listRowEntryEntry, listRowEntryPayments);
           workSheet.generateReport();
           listRowEntries = workSheet.getRowEntrys();
           laundry = laundryManager.get(laundry.getId());
           periodPaymentLaundryList = laundry.getPeriodPaymentLaundryList();
        }
        return "generateReportLaundry";
    }
    public List<RowEntry> getListRowEntryEntry(List<LaundryRequest> paymenRequestList){
        List<RowEntry> res = new ArrayList<RowEntry>();
        for (Iterator<LaundryRequest> it = paymenRequestList.iterator(); it.hasNext();) {
            LaundryRequest lr = it.next();
            RowEntry re = new RowEntry();
            re.setId(lr.getId());
            re.setTable("laundry_request");
            re.setEntry(lr.getTotalPrice());
            res.add(re);
        }
        return res;
    }
    public List<RowEntry> getListRowEntryPayment(List<LaundryPayment> paymenLaundryList){
        List<RowEntry> res = new ArrayList<RowEntry>();
        for (Iterator<LaundryPayment> it = paymenLaundryList.iterator(); it.hasNext();) {
            LaundryPayment lp = it.next();
            RowEntry re = new RowEntry();
            re.setId(lp.getId());
            re.setTable("laundry_payment");
            re.setPayment(lp.getAmount());
            res.add(re);
        }
        return res;
    }
    public List<Laundry> getLaundryList() {
        return laundryList;
    }

    public void setLaundryList(List<Laundry> laundryList) {
        this.laundryList = laundryList;
    }

    public LaundryManager getLaundryManager() {
        return laundryManager;
    }

    public void setLaundryManager(LaundryManager laundryManager) {
        this.laundryManager = laundryManager;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Laundry getLaundry() {
        return laundry;
    }

    public void setLaundry(Laundry laundry) {
        this.laundry = laundry;
    }

    public List<PeriodPaymentLaundry> getPeriodPaymentLaundryList() {
        return periodPaymentLaundryList;
    }

    public void setPeriodPaymentLaundryList(List<PeriodPaymentLaundry> periodPaymentLaundryList) {
        this.periodPaymentLaundryList = periodPaymentLaundryList;
    }

    public String getChoicedPerPayLaundry() {
        return choicedPerPayLaundry;
    }

    public void setChoicedPerPayLaundry(String choicedPerPayLaundry) {
        this.choicedPerPayLaundry = choicedPerPayLaundry;
    }

    public PeriodPaymentLaundry getPeriodPaymentLaundry() {
        return periodPaymentLaundry;
    }

    public void setPeriodPaymentLaundry(PeriodPaymentLaundry periodPaymentLaundry) {
        this.periodPaymentLaundry = periodPaymentLaundry;
    }

    public PeriodPaymentLaundryManager getPeriodPaymentLaundryManager() {
        return periodPaymentLaundryManager;
    }

    public void setPeriodPaymentLaundryManager(PeriodPaymentLaundryManager periodPaymentLaundryManager) {
        this.periodPaymentLaundryManager = periodPaymentLaundryManager;
    }

    public List<RowEntry> getListRowEntries() {
        return listRowEntries;
    }

    public void setListRowEntries(List<RowEntry> listRowEntries) {
        this.listRowEntries = listRowEntries;
    }
    
}
